home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / utilities / isfast / libtk / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.8 KB  |  324 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <X11/keysym.h>
  4. #include "tk.h"
  5. #include "private.h"
  6.  
  7. #if defined(__cplusplus) || defined(c_plusplus)
  8. #define class c_class
  9. #endif
  10.  
  11. /******************************************************************************/
  12.  
  13. void (*ExposeFunc)(int, int) = 0;
  14. void (*ReshapeFunc)(int, int) = 0;
  15. void (*DisplayFunc)(void) = 0;
  16. GLenum (*KeyDownFunc)(int, GLenum) = 0;
  17. GLenum (*MouseDownFunc)(int, int, GLenum) = 0;
  18. GLenum (*MouseUpFunc)(int, int, GLenum) = 0;
  19. GLenum (*MouseMoveFunc)(int, int, GLenum) = 0;
  20. void (*IdleFunc)(void) = 0;
  21. int lastEventType = -1;
  22. int tkLoopFlag;
  23.  
  24. /******************************************************************************/
  25.  
  26. static GLenum DoNextEvent(void)
  27. {
  28.     XEvent current, ahead;
  29.     char buf[1000];
  30.     KeySym ks;
  31.     int key;
  32.  
  33.     XNextEvent(xDisplay, ¤t);
  34.     switch (current.type) {
  35.       case MappingNotify:
  36.     XRefreshKeyboardMapping((XMappingEvent *)¤t);
  37.     lastEventType = MappingNotify;
  38.     return GL_FALSE;
  39.       
  40.       case ClientMessage:
  41.     if (current.xclient.data.l[0] == deleteWindowAtom) {
  42.         exit(0);
  43.     }
  44.     return GL_FALSE;
  45.  
  46.       case Expose:
  47.     while (XEventsQueued(current.xexpose.display, QueuedAfterReading) > 0) {
  48.         XPeekEvent(current.xexpose.display, &ahead);
  49.         if (ahead.xexpose.window != current.xexpose.window ||
  50.         ahead.type != Expose) {
  51.         break;
  52.         }
  53.         XNextEvent(xDisplay, ¤t);
  54.     }
  55.     if (current.xexpose.count == 0) {
  56.         if (ExposeFunc) {
  57.         (*ExposeFunc)(w.w, w.h);
  58.         if (lastEventType == ConfigureNotify) {
  59.             lastEventType = Expose;
  60.             return GL_FALSE;
  61.         } else {
  62.             lastEventType = Expose;
  63.             return GL_TRUE;
  64.         }
  65.         }
  66.     }
  67.     return GL_FALSE;
  68.  
  69.       case ConfigureNotify:
  70.     lastEventType = ConfigureNotify;
  71.     w.w = current.xconfigure.width;
  72.     w.h = current.xconfigure.height;
  73.     if (ReshapeFunc) {
  74.         (*ReshapeFunc)(w.w, w.h);
  75.         return GL_TRUE;
  76.     } else {
  77.         return GL_FALSE;
  78.     }
  79.  
  80.       case MotionNotify:
  81.     lastEventType = MotionNotify;
  82.     if (MouseMoveFunc) {
  83.         GLenum mask;
  84.  
  85.         mask = 0;
  86.         if (current.xmotion.state & Button1Mask) {
  87.         mask |= TK_LEFTBUTTON;
  88.         }
  89.         if (current.xmotion.state & Button2Mask) {
  90.         mask |= TK_MIDDLEBUTTON;
  91.         }
  92.         if (current.xmotion.state & Button3Mask) {
  93.         mask |= TK_RIGHTBUTTON;
  94.         }
  95.         return (*MouseMoveFunc)(current.xmotion.x, current.xmotion.y, mask);
  96.     } else {
  97.         return GL_FALSE;
  98.     }
  99.  
  100.       case ButtonPress:
  101.     lastEventType = ButtonPress;
  102.     if (MouseDownFunc) {
  103.         GLenum mask;
  104.  
  105.         mask = 0;
  106.         if (current.xbutton.button == 1) {
  107.         mask |= TK_LEFTBUTTON;
  108.         }
  109.         if (current.xbutton.button == 2) {
  110.         mask |= TK_MIDDLEBUTTON;
  111.         }
  112.         if (current.xbutton.button == 3) {
  113.         mask |= TK_RIGHTBUTTON;
  114.         }
  115.         return (*MouseDownFunc)(current.xbutton.x, current.xbutton.y, mask);
  116.     } else {
  117.         return GL_FALSE;
  118.     }
  119.       case ButtonRelease:
  120.     lastEventType = ButtonRelease;
  121.     if (MouseUpFunc) {
  122.         GLenum mask;
  123.  
  124.         mask = 0;
  125.         if (current.xbutton.button == 1) {
  126.         mask |= TK_LEFTBUTTON;
  127.         }
  128.         if (current.xbutton.button == 2) {
  129.         mask |= TK_MIDDLEBUTTON;
  130.         }
  131.         if (current.xbutton.button == 3) {
  132.         mask |= TK_RIGHTBUTTON;
  133.         }
  134.         return (*MouseUpFunc)(current.xbutton.x, current.xbutton.y, mask);
  135.     } else {
  136.         return GL_FALSE;
  137.     }
  138.  
  139.       case KeyPress:
  140.     lastEventType = KeyPress;
  141.     XLookupString(¤t.xkey, buf, sizeof(buf), &ks, 0);
  142.     switch (ks) {
  143.       case XK_0:         key = TK_0;        break;
  144.       case XK_1:         key = TK_1;        break;
  145.       case XK_2:         key = TK_2;        break;
  146.       case XK_3:         key = TK_3;        break;
  147.       case XK_4:         key = TK_4;        break;
  148.       case XK_5:         key = TK_5;        break;
  149.       case XK_6:         key = TK_6;        break;
  150.       case XK_7:         key = TK_7;        break;
  151.       case XK_8:         key = TK_8;        break;
  152.       case XK_9:         key = TK_9;        break;
  153.       case XK_A:         key = TK_A;        break;
  154.       case XK_B:         key = TK_B;        break;
  155.       case XK_C:         key = TK_C;        break;
  156.       case XK_D:         key = TK_D;        break;
  157.       case XK_E:         key = TK_E;        break;
  158.       case XK_F:         key = TK_F;        break;
  159.       case XK_G:         key = TK_G;        break;
  160.       case XK_H:         key = TK_H;        break;
  161.       case XK_I:         key = TK_I;        break;
  162.       case XK_J:         key = TK_J;        break;
  163.       case XK_K:         key = TK_K;        break;
  164.       case XK_L:         key = TK_L;        break;
  165.       case XK_M:         key = TK_M;        break;
  166.       case XK_N:         key = TK_N;        break;
  167.       case XK_O:         key = TK_O;        break;
  168.       case XK_P:         key = TK_P;        break;
  169.       case XK_Q:         key = TK_Q;        break;
  170.       case XK_R:         key = TK_R;        break;
  171.       case XK_S:         key = TK_S;        break;
  172.       case XK_T:         key = TK_T;        break;
  173.       case XK_U:         key = TK_U;        break;
  174.       case XK_V:         key = TK_V;        break;
  175.       case XK_W:         key = TK_W;        break;
  176.       case XK_X:         key = TK_X;        break;
  177.       case XK_Y:         key = TK_Y;        break;
  178.       case XK_Z:         key = TK_Z;        break;
  179.       case XK_a:         key = TK_a;        break;
  180.       case XK_b:         key = TK_b;        break;
  181.       case XK_c:         key = TK_c;        break;
  182.       case XK_d:         key = TK_d;        break;
  183.       case XK_e:         key = TK_e;        break;
  184.       case XK_f:         key = TK_f;        break;
  185.       case XK_g:         key = TK_g;        break;
  186.       case XK_h:         key = TK_h;        break;
  187.       case XK_i:         key = TK_i;        break;
  188.       case XK_j:         key = TK_j;        break;
  189.       case XK_k:         key = TK_k;        break;
  190.       case XK_l:         key = TK_l;        break;
  191.       case XK_m:         key = TK_m;        break;
  192.       case XK_n:         key = TK_n;         break;
  193.       case XK_o:         key = TK_o;        break;
  194.       case XK_p:         key = TK_p;        break;
  195.       case XK_q:         key = TK_q;        break;
  196.       case XK_r:         key = TK_r;        break;
  197.       case XK_s:         key = TK_s;        break;
  198.       case XK_t:         key = TK_t;        break;
  199.       case XK_u:         key = TK_u;        break;
  200.       case XK_v:         key = TK_v;        break;
  201.       case XK_w:         key = TK_w;        break;
  202.       case XK_x:         key = TK_x;        break;
  203.       case XK_y:         key = TK_y;        break;
  204.       case XK_z:         key = TK_z;        break;
  205.       case XK_space:    key = TK_SPACE;        break;
  206.       case XK_Return:     key = TK_RETURN;    break;
  207.       case XK_Escape:     key = TK_ESCAPE;    break;
  208.       case XK_Left:        key = TK_LEFT;        break;
  209.       case XK_Up:        key = TK_UP;        break;
  210.       case XK_Right:      key = TK_RIGHT;        break;
  211.       case XK_Down:        key = TK_DOWN;        break;
  212.       default:         key = GL_FALSE;        break;
  213.     }
  214.     if (key && KeyDownFunc) {
  215.         GLenum mask;
  216.  
  217.         mask = 0;
  218.         if (current.xkey.state & ControlMask) {
  219.         mask |= TK_CONTROL;
  220.         }
  221.         if (current.xkey.state & ShiftMask) {
  222.         mask |= TK_SHIFT;
  223.         }
  224.         return (*KeyDownFunc)(key, mask);
  225.     } else {
  226.         return GL_FALSE;
  227.     }
  228.     }
  229.     return GL_FALSE;
  230. }
  231.  
  232. void tkExec(void)
  233. {
  234.     GLenum flag;
  235.  
  236.     tkLoopFlag = 1;
  237.  
  238.     while (tkLoopFlag) {
  239.     if (IdleFunc) {
  240.         if (IdleFunc) {
  241.         (*IdleFunc)();
  242.         }
  243.         while (XPending(xDisplay)) {
  244.         flag |= DoNextEvent();
  245.         }
  246.         if (DisplayFunc) {
  247.         (*DisplayFunc)();
  248.         }
  249.     } else {
  250.         if (DoNextEvent() == GL_TRUE) {
  251.         if (DisplayFunc) {
  252.             (*DisplayFunc)();
  253.         }
  254.         }
  255.     }
  256.     }
  257. }
  258.  
  259. /******************************************************************************/
  260.  
  261. void tkExposeFunc(void (*Func)(int, int))
  262. {
  263.  
  264.     ExposeFunc = Func;
  265. }
  266.  
  267. /******************************************************************************/
  268.  
  269. void tkReshapeFunc(void (*Func)(int, int))
  270. {
  271.  
  272.     ReshapeFunc = Func;
  273. }
  274.  
  275. /******************************************************************************/
  276.  
  277. void tkDisplayFunc(void (*Func)(void))
  278. {
  279.  
  280.     DisplayFunc = Func;
  281. }
  282.  
  283. /******************************************************************************/
  284.  
  285. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  286. {
  287.  
  288.     KeyDownFunc = Func;
  289. }
  290.  
  291. /******************************************************************************/
  292.  
  293. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  294. {
  295.  
  296.     MouseDownFunc = Func;
  297. }
  298.  
  299. /******************************************************************************/
  300.  
  301. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  302. {
  303.  
  304.     MouseUpFunc = Func;
  305. }
  306.  
  307. /******************************************************************************/
  308.  
  309. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  310. {
  311.  
  312.     MouseMoveFunc = Func;
  313. }
  314.  
  315. /******************************************************************************/
  316.  
  317. void tkIdleFunc(void (*Func)(void))
  318. {
  319.  
  320.     IdleFunc = Func;
  321. }
  322.  
  323. /******************************************************************************/
  324.